package com.applink.sell_proof.database.table;

import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;

import com.applink.sell_proof.EMarketing;
import com.applink.sell_proof.database.EMarketingContentProvider;
import com.applink.sell_proof.model.PriceBook;
import com.applink.sell_proof.util.Utility;

public class PriceBookDBHelper {

	public static final String TABLE_NAME = "pricebook_table";
	public static final String PB_ID = "PriceBookID";
	public static final String PB_YEAR = "PriceBkYear";
	public static final String PB_MONTH = "PriceBkMonth";
	public static final String PB_CATEGORY = "PriceBkCategory";
	public static final String PB_IMAGE_NAME = "PriceBkImageName";
	public static final String PB_IMAGE_PATH = "PriceBkImagePath";
	public static final String PB_IMAGE_PATH_LOCAL = "PriceImageLocal";
	public static final String PB_DATETIME_SAVED = "PriceBookDateTimeSaved";

	public static final String SQL_EXCUTE = "CREATE TABLE " + TABLE_NAME + "("
			+ PB_ID + " integer primary key ," + PB_YEAR + " text ," + PB_MONTH
			+ " int ," + PB_CATEGORY + " text ," + PB_IMAGE_NAME + " text ,"
			+ PB_IMAGE_PATH + " text ," + PB_IMAGE_PATH_LOCAL + " text, "
			+ PB_DATETIME_SAVED + " DATETIME);";

	public static void createPriceBook(final PriceBook priceBook) {
		new Thread() {
			@Override
			public void run() {
				super.run();
				ContentValues value = new ContentValues();
				ContentResolver resolver = EMarketing.getInstance()
						.getApplicationContext().getContentResolver();
				value.put(PB_ID, priceBook.getPriceBookID());
				value.put(PB_YEAR, priceBook.getPriceBkYear());
				value.put(PB_MONTH, priceBook.getPriceBkMonth());
				value.put(PB_CATEGORY, priceBook.getSaleCategoryName());
				value.put(PB_IMAGE_NAME, priceBook.getPriceBookName());
				value.put(PB_IMAGE_PATH, priceBook.getPhysicalPath());
				value.put(PB_IMAGE_PATH_LOCAL, priceBook.getPhysicalPathLocal());
				value.put(PB_DATETIME_SAVED, priceBook.getDownloadedDate());
				resolver.insert(
						EMarketingContentProvider.PRICEBOOK_CONTENT_URI, value);
				Log.v("nglehien", "pricebook: " + priceBook.getPriceBookID());
			}

		}.start();
	}

	public static ArrayList<PriceBook> getAllPriceBook() {
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		Cursor cursor = resolver.query(
				EMarketingContentProvider.PRICEBOOK_CONTENT_URI, null, null,
				null, PB_ID + " DESC ");

		if (cursor != null) {
			ArrayList<PriceBook> priBooks = new ArrayList<PriceBook>();
			while (cursor.moveToNext()) {
				PriceBook priceBook = new PriceBook();
				priceBook.setPriceBookID(cursor.getInt(cursor
						.getColumnIndex(PB_ID)));
				priceBook.setPriceBkYear(cursor.getString(cursor
						.getColumnIndex(PB_YEAR)));
				priceBook.setPriceBkMonth(cursor.getInt(cursor
						.getColumnIndex(PB_MONTH)));
				priceBook.setSaleCategoryName(cursor.getString(cursor
						.getColumnIndex(PB_CATEGORY)));
				priceBook.setPriceBookName(cursor.getString(cursor
						.getColumnIndex(PB_IMAGE_NAME)));
				priceBook.setPhysicalPath(cursor.getString(cursor
						.getColumnIndex(PB_IMAGE_PATH)));
				priceBook.setPhysicalPathLocal(cursor.getString(cursor
						.getColumnIndex(PB_IMAGE_PATH_LOCAL)));
				priBooks.add(priceBook);
			}
			return priBooks.size() > 0 ? priBooks : null;
		}
		return null;
	}

	public static void updatePriceBook(PriceBook priceBook) {
		ContentValues value = new ContentValues();
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		value.put(PB_ID, priceBook.getPriceBookID());
		value.put(PB_YEAR, priceBook.getPriceBkYear());
		value.put(PB_MONTH, priceBook.getPriceBkMonth());
		value.put(PB_CATEGORY, priceBook.getSaleCategoryName());
		value.put(PB_IMAGE_NAME, priceBook.getPriceBookName());
		value.put(PB_IMAGE_PATH, priceBook.getPhysicalPath());
		value.put(PB_IMAGE_PATH_LOCAL, priceBook.getPhysicalPathLocal());

		resolver.update(EMarketingContentProvider.PRICEBOOK_CONTENT_URI, value,
				PB_ID + "=?",
				new String[] { Long.toString(priceBook.getPriceBookID()) });

	}

	public static void deleteAllPricebook() {
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		ArrayList<PriceBook> priceBooks = getAllPriceBook();
		if (priceBooks != null) {
			for (PriceBook priceBook : priceBooks) {
				int result = resolver.delete(
						EMarketingContentProvider.PRICEBOOK_CONTENT_URI, PB_ID
								+ "=?", new String[] { Long.toString(priceBook
								.getPriceBookID()) });
				if (result > 0) {
					Utility.deleteFile(priceBook.getPhysicalPathLocal());
				}
			}
		}
	}

	public static void deletePriceBook() {
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		resolver.delete(EMarketingContentProvider.PRICEBOOK_CONTENT_URI, null,
				null);
	}

	public static void deletePriceBook(int Id) {
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		resolver.delete(EMarketingContentProvider.PRICEBOOK_CONTENT_URI, PB_ID
				+ "=?", new String[] { Integer.toString(Id) });
	}

	public static boolean isPriceBookExists(int priceBookId) {
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		Cursor cursor = resolver.query(
				EMarketingContentProvider.PRICEBOOK_CONTENT_URI, null, PB_ID
						+ "=?", new String[] { Integer.toString(priceBookId) },
				null);
		if (cursor != null) {
			boolean isExists = cursor.getCount() > 0;
			if (isExists) {
				while (cursor.moveToNext()) {
					String fileUrl = cursor.getString(cursor
							.getColumnIndex(PB_IMAGE_PATH_LOCAL));
					if (fileUrl == null || fileUrl.length() == 0
							|| !Utility.isFileExists(fileUrl)) {
						deletePriceBook(priceBookId);
						return false;
					}
				}
				return true;
			}
			cursor.close();
			return false;
		}
		return false;
	}
}
